package org.gdstash.ui.character;

import org.gdstash.character.GDChar;
import org.gdstash.character.GDCharTeleportList;
import org.gdstash.character.GDCharUID;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.GDAbstractRiftPane;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;

public class GDCharRiftAct5Pane extends GDAbstractRiftPane {
    private JCheckBox cbGloomwald;
    private JCheckBox cbGloomwaldCrossing;
    private JCheckBox cbCovensRefuge;
    private JCheckBox cbUgdenbog;
    private JCheckBox cbBarrowholm;
    private JCheckBox cbLoneWatch;
    private JCheckBox cbMalmouthOutskirts;
    private JCheckBox cbMalmouthSteelcap;
    private JCheckBox cbMalmouthCrownHill;
    private JCheckBox cbMalmouthSewers;
    private int difficulty;
    private boolean changed;

    public GDCharRiftAct5Pane(final int difficulty, final int direction) {
        this.difficulty = difficulty;
        this.changed = false;
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.cbGloomwald).addComponent(this.cbGloomwaldCrossing).addComponent(this.cbCovensRefuge).addComponent(this.cbUgdenbog).addComponent(this.cbBarrowholm).addComponent(this.cbLoneWatch).addComponent(this.cbMalmouthOutskirts).addComponent(this.cbMalmouthSewers).addComponent(this.cbMalmouthSteelcap).addComponent(this.cbMalmouthCrownHill));
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.cbGloomwald)).addGroup(layout.createParallelGroup().addComponent(this.cbGloomwaldCrossing)).addGroup(layout.createParallelGroup().addComponent(this.cbCovensRefuge)).addGroup(layout.createParallelGroup().addComponent(this.cbUgdenbog)).addGroup(layout.createParallelGroup().addComponent(this.cbBarrowholm)).addGroup(layout.createParallelGroup().addComponent(this.cbLoneWatch)).addGroup(layout.createParallelGroup().addComponent(this.cbMalmouthOutskirts)).addGroup(layout.createParallelGroup().addComponent(this.cbMalmouthSewers)).addGroup(layout.createParallelGroup().addComponent(this.cbMalmouthSteelcap)).addGroup(layout.createParallelGroup().addComponent(this.cbMalmouthCrownHill));
        if (direction == 0) {
            layout.setHorizontalGroup(vGroup);
            layout.setVerticalGroup(hGroup);
        } else {
            layout.setHorizontalGroup(hGroup);
            layout.setVerticalGroup(vGroup);
        }
        layout.linkSize(0, this.cbGloomwald, this.cbGloomwaldCrossing);
        layout.linkSize(0, this.cbGloomwald, this.cbCovensRefuge);
        layout.linkSize(0, this.cbGloomwald, this.cbUgdenbog);
        layout.linkSize(0, this.cbGloomwald, this.cbBarrowholm);
        layout.linkSize(0, this.cbGloomwald, this.cbLoneWatch);
        layout.linkSize(0, this.cbGloomwald, this.cbMalmouthOutskirts);
        layout.linkSize(0, this.cbGloomwald, this.cbMalmouthSewers);
        layout.linkSize(0, this.cbGloomwald, this.cbMalmouthSteelcap);
        layout.linkSize(0, this.cbGloomwald, this.cbMalmouthCrownHill);
        layout.linkSize(1, this.cbGloomwald, this.cbGloomwaldCrossing);
        layout.linkSize(1, this.cbGloomwald, this.cbCovensRefuge);
        layout.linkSize(1, this.cbGloomwald, this.cbUgdenbog);
        layout.linkSize(1, this.cbGloomwald, this.cbBarrowholm);
        layout.linkSize(1, this.cbGloomwald, this.cbLoneWatch);
        layout.linkSize(1, this.cbGloomwald, this.cbMalmouthOutskirts);
        layout.linkSize(1, this.cbGloomwald, this.cbMalmouthSewers);
        layout.linkSize(1, this.cbGloomwald, this.cbMalmouthSteelcap);
        layout.linkSize(1, this.cbGloomwald, this.cbMalmouthCrownHill);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntCheck = UIManager.getDefaults().getFont("CheckBox.font");
        if (fntCheck == null) {
            fntCheck = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntCheck = fntCheck.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ACT5"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.cbGloomwald == null) {
            (this.cbGloomwald = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbGloomwald.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_05_GLOOMWALD"));
        this.cbGloomwald.setFont(fntCheck);
        if (this.cbGloomwaldCrossing == null) {
            (this.cbGloomwaldCrossing = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbGloomwaldCrossing.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_05_GLOOMWALD_CROSSING"));
        this.cbGloomwaldCrossing.setFont(fntCheck);
        if (this.cbCovensRefuge == null) {
            (this.cbCovensRefuge = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbCovensRefuge.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_05_COVENS_REFUGE"));
        this.cbCovensRefuge.setFont(fntCheck);
        if (this.cbUgdenbog == null) {
            (this.cbUgdenbog = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbUgdenbog.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_05_UGDENBOG"));
        this.cbUgdenbog.setFont(fntCheck);
        if (this.cbBarrowholm == null) {
            (this.cbBarrowholm = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbBarrowholm.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_05_BARROWHOLM"));
        this.cbBarrowholm.setFont(fntCheck);
        if (this.cbLoneWatch == null) {
            (this.cbLoneWatch = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbLoneWatch.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_05_LONE_WATCH"));
        this.cbLoneWatch.setFont(fntCheck);
        if (this.cbMalmouthOutskirts == null) {
            (this.cbMalmouthOutskirts = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbMalmouthOutskirts.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_05_MALMOUTH_OUTSKIRTS"));
        this.cbMalmouthOutskirts.setFont(fntCheck);
        if (this.cbMalmouthSewers == null) {
            (this.cbMalmouthSewers = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbMalmouthSewers.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_05_MALMOUTH_SEWERS"));
        this.cbMalmouthSewers.setFont(fntCheck);
        if (this.cbMalmouthSteelcap == null) {
            (this.cbMalmouthSteelcap = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbMalmouthSteelcap.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_05_MALMOUTH_STEELCAP"));
        this.cbMalmouthSteelcap.setFont(fntCheck);
        if (this.cbMalmouthCrownHill == null) {
            (this.cbMalmouthCrownHill = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbMalmouthCrownHill.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_05_MALMOUTH_CROWN_HILL"));
        this.cbMalmouthCrownHill.setFont(fntCheck);
    }

    @Override
    public boolean hasChanged() {
        return this.changed;
    }

    @Override
    public void setChanged(final boolean changed) {
        this.changed = changed;
    }

    public void setSelected(final boolean selected) {
        this.cbGloomwald.setSelected(selected);
        this.cbGloomwaldCrossing.setSelected(selected);
        this.cbCovensRefuge.setSelected(selected);
        this.cbUgdenbog.setSelected(selected);
        this.cbBarrowholm.setSelected(selected);
        this.cbLoneWatch.setSelected(selected);
        this.cbMalmouthOutskirts.setSelected(selected);
        this.cbMalmouthSewers.setSelected(selected);
        this.cbMalmouthSteelcap.setSelected(selected);
        this.cbMalmouthCrownHill.setSelected(selected);
    }

    @Override
    public List<GDCharUID> getRiftList(final boolean found) {
        final List<GDCharUID> list = new LinkedList<GDCharUID>();
        if (this.cbGloomwald.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_05_GLOOMWALD);
        }
        if (this.cbGloomwaldCrossing.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_05_GLOOMWALD_CROSSING);
        }
        if (this.cbCovensRefuge.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_05_COVENS_REFUGE);
        }
        if (this.cbUgdenbog.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_05_UGDENBOG);
        }
        if (this.cbBarrowholm.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_05_BARROWHOLM);
        }
        if (this.cbLoneWatch.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_05_LONE_WATCH);
        }
        if (this.cbMalmouthOutskirts.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_05_MALMOUTH_OUTSKIRTS);
        }
        if (this.cbMalmouthSewers.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_05_MALMOUTH_SEWERS);
        }
        if (this.cbMalmouthSteelcap.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_05_MALMOUTH_STEELCAP);
        }
        if (this.cbMalmouthCrownHill.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_05_MALMOUTH_CROWN_HILL);
        }
        return list;
    }

    @Override
    public void setChar(final GDChar gdc) {
        this.setSelected(false);
        this.changed = false;
        boolean expAoM = GDStashFrame.expansionAshesOfMalmouth;
        if (gdc != null) {
            expAoM = (expAoM || gdc.isAsheshOfMalmouthChar() || gdc.isForgottenGodsChar());
        }
        if (expAoM) {
            this.cbGloomwald.setEnabled(true);
            this.cbGloomwaldCrossing.setEnabled(true);
            this.cbCovensRefuge.setEnabled(true);
            this.cbUgdenbog.setEnabled(true);
            this.cbBarrowholm.setEnabled(true);
            this.cbLoneWatch.setEnabled(true);
            this.cbMalmouthOutskirts.setEnabled(true);
            this.cbMalmouthSewers.setEnabled(true);
            this.cbMalmouthSteelcap.setEnabled(true);
            this.cbMalmouthCrownHill.setEnabled(true);
        } else {
            this.cbGloomwald.setEnabled(false);
            this.cbGloomwaldCrossing.setEnabled(false);
            this.cbCovensRefuge.setEnabled(false);
            this.cbUgdenbog.setEnabled(false);
            this.cbBarrowholm.setEnabled(false);
            this.cbLoneWatch.setEnabled(false);
            this.cbMalmouthOutskirts.setEnabled(false);
            this.cbMalmouthSewers.setEnabled(false);
            this.cbMalmouthSteelcap.setEnabled(false);
            this.cbMalmouthCrownHill.setEnabled(false);
        }
        if (gdc == null) {
            return;
        }
        final List<GDCharUID> list = gdc.getRiftList(this.difficulty);
        for (final GDCharUID uid : list) {
            if (uid.equals(GDCharTeleportList.UID_RIFT_05_GLOOMWALD)) {
                this.cbGloomwald.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_05_GLOOMWALD_CROSSING)) {
                this.cbGloomwaldCrossing.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_05_COVENS_REFUGE)) {
                this.cbCovensRefuge.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_05_UGDENBOG)) {
                this.cbUgdenbog.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_05_BARROWHOLM)) {
                this.cbBarrowholm.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_05_LONE_WATCH)) {
                this.cbLoneWatch.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_05_MALMOUTH_OUTSKIRTS)) {
                this.cbMalmouthOutskirts.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_05_MALMOUTH_SEWERS)) {
                this.cbMalmouthSewers.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_05_MALMOUTH_STEELCAP)) {
                this.cbMalmouthSteelcap.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_05_MALMOUTH_CROWN_HILL)) {
                this.cbMalmouthCrownHill.setSelected(true);
            }
        }
    }

    private class ChangeActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            GDCharRiftAct5Pane.this.changed = true;
        }
    }
}
